package com.atguigu.srb.sms.controller.api;

import com.atguigu.common.result.R;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.utils.SmsProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author study
 * @version 1.0
 * @date 2021/4/30 15:16
 */
@RestController
@RequestMapping("/api/sms")
// @CrossOrigin
@Api(tags = "短信管理")
@Slf4j
public class ApiSmsController {

    @Resource
    private SmsService smsService;

    @Resource
    private CoreUserInfoClient coreUserInfoClient;

    @GetMapping("/send/{mobile}")
    public R send(
            @ApiParam(value = "手机号", required = true)
            @PathVariable String mobile) {
        final boolean b = coreUserInfoClient.checkMobile(mobile);
        if (b) {
            return R.error().message("已经注册");
        }
        final HashMap<String, String> map = new HashMap<>(1);
        map.put("code", RandomStringUtils.randomNumeric(4));
        smsService.send(mobile, SmsProperties.TEMPLATE_CODE, map);
        return R.ok().message("短信发送成功");
    }
}
